/**
 * 
 */
package craneding.gwt.databean.client.ui.field;

import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.Widget;

import craneding.gwt.databean.client.model.FieldInfo;

/**
 * like Image
 * <h3>CSS Style Rules</h3>
 * <ul class='css'>
 * <li>.DataBean-ImageFieldInput{ primary style }</li>
 * </ul>
 * @author crane.ding
 * @version 1.0 2008-12-2
 */
public class ImageFieldInput extends Image implements IFieldInput {
	private FieldInfo fieldInfo;
	@SuppressWarnings("unused")
	private String fieldName;

	public ImageFieldInput() {
		addLoadListener(new LoadListener() {
			int times = 0;

			public void onError(Widget sender) {
				if (++times < 1)
					setUrl("images/defaultImage.cache.jpg");
			}

			public void onLoad(Widget sender) {
			}
		});
		setStyleName("DataBean-ImageFieldInput");
	}

	public Object getValue() {
		return getUrl().trim();
	}

	public void setFieldInfo(String fieldName, FieldInfo fieldInfo) {
		this.fieldName = fieldName;
		this.fieldInfo = fieldInfo;

		setMetaData();
	}

	private void setMetaData() {
		String description = this.fieldInfo.description();
		if (description.length() > 0)
			setTitle(description);
	}

	public void setValue(Object value) {
		if (value == null || value.toString().trim().equals(""))
			setUrl("images/defaultImage.cache.jpg");
		else
			setUrl(value.toString());
	}

	public void validityCheck() throws ValidationException {
		
	}

}
